﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lidgren.Network;
using System.Drawing;
using Model.LiveControl;
using Network;
using Network.Messages.LiveControl;

namespace Providers.LiveControl.Server.ScreenHelper
{
    public class ClientRequestParam 
    {

        public ClientRequestParam(MessageEventArgs<RequestScreenshotMessage> args)
        {
            Connection = args.Connection;
            Message = args.Message;
        }

        public NetConnection Connection;
        public RequestScreenshotMessage Message;
        public Rectangle RectBound = Rectangle.Empty;

        public void GetRectAndSend(NetworkPeer network)
        {
            if (Message.Id <= 0) return;
            var rect = WindowApi.GetRect(Message.Id);


            OffsetRect(rect, Screen.AllScreens[Message.ScreenIndex].Bounds);
            RectBound = rect.Rect.ToRectangle();
            network.SendMessage(
                new ResponseRectChangeMessage { Rect = rect },
                NetDeliveryMethod.ReliableOrdered,
                3,
                Connection);
        }
        private static void OffsetRect(IdRect ret, Rectangle rectScreen)
        {
            ret.Rect.Left -= rectScreen.X;
            ret.Rect.Right -= rectScreen.X;
            ret.Rect.Top -= rectScreen.Y;
            ret.Rect.Bottom -= rectScreen.Y;
        }
        public bool CheckSend(Rectangle tileRect,bool changed)
        {
            // 如果是首帧，则不管是否发生变化，都要发送
            if (Message.FlagFirstFrame)
            {
                return true;
            }

            // 变化区域对该连接有效
            var effectiveChanged = (RectBound.IsEmpty || RectBound.IntersectsWith(tileRect)) && changed;
            return effectiveChanged;
        }

    }
}
